import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.IOException;

/**
 * @Author： yu.Tan
 * @Date: 2020/8/20 9:58
 * @Description:
 * #: 代表一个或者多个
 * *: 代表一个
 */
public class ProducerTopic {

    private static final String EXCHANGE_NAME = "exchange_topic";

    //交换机类型：topic 类似正则匹配模式
    private static final String EXCHANGE_TYPE = "topic";

    //指定routing key
    private static final String EXCHANGE_ROUTE_KEY = "news.insert.1";

    public static void main(String[] args) {
        Connection connection = MQConnecitonUtils.getConnection();
        Channel channel = null;

        try {
            channel = connection.createChannel();

            channel.exchangeDeclare(EXCHANGE_NAME, EXCHANGE_TYPE);

            String msg = "Hello,World1";
            channel.basicPublish(EXCHANGE_NAME, EXCHANGE_ROUTE_KEY, null, msg.getBytes("UTF-8"));

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != channel) {
                try {
                    channel.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (null != connection) {
                try {
                    connection.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
